package com.hk.webapp;

import com.hk.webapp.res_config.SwaggerProperty;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

import java.util.List;

import static springfox.documentation.builders.PathSelectors.regex;

@ConditionalOnExpression("${custom.swagger.show}")
@EnableSwagger2
@Configuration
public class Swagger2 {

    @Resource
    private SwaggerProperty swaggerProperty;

    //创建api应用
    //apiInfo()增加api信息
    //通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现，
    //本例采用指定扫描的包路径来定义指定要建立API的目录。
    @Bean
    public Docket apiDocument() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .groupName(swaggerProperty.getClientTitle())
                .select()
                //Swagger扫描包的路径
                //api.class这里是全局扫描有@Api注解得类，还可以扫描任意位置，指定包以及针对方法上的指定注解
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(regex("/api/(?!admin).*"))
                .build()
                .pathMapping("/")
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .apiInfo(clientMetadata());
    }

    @Bean
    public Docket pubDocument() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .groupName(swaggerProperty.getAdminTitle())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(regex("/api/admin.*"))
                .build()
                .pathMapping("/")
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts())
                .apiInfo(adminMetadata());
    }

//    @Bean
//    public Docket testDocument() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .useDefaultResponseMessages(false)
//                .groupName(swaggerProperty.getHkTest())
//                .select()
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//                .paths(regex("/api/admin.*"))
//                .build()
//                .pathMapping("/")
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts())
//                .apiInfo(testMetadata());
//    }
//    private ApiInfo testMetadata() {
//        return new ApiInfoBuilder()
//                .title(swaggerProperty.getHkTest())
//                .version(swaggerProperty.getVersion())
//                .contact(new Contact("lq","http:baidu.com","370280849@qq.com"))
//                .build();
//    }
    private ApiInfo clientMetadata() {
        return new ApiInfoBuilder()
                .title(swaggerProperty.getClientTitle())
                .version(swaggerProperty.getVersion())
                .contact(new Contact("lq","http:baidu.com","370280849@qq.com"))
                .build();
    }

    private ApiInfo adminMetadata() {
        return new ApiInfoBuilder()
                .title(swaggerProperty.getAdminTitle())
                .version(swaggerProperty.getVersion())
                .contact(new Contact("lq","http:baidu.com","370280849@qq.com"))
                .build();
    }


    private List<ApiKey> securitySchemes() {
        List<ApiKey> list = Lists.newArrayList();
        list.add(new ApiKey("Authorization", "Authorization", "header"));
        return list;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> list = Lists.newArrayList();
        list.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!api).*$"))
                .build());
        return list;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> list = Lists.newArrayList();
        list.add(new SecurityReference("Authorization", authorizationScopes));
        return list;
    }


}
